import api from "@/fetch/api.js"

const state={
     user: {
        id: '',
        username: '',
        nickname: '',
        password: '',
        sex: '',
        city: '',
        email: '',
        sign: ''
     }
}

const getters={
    user(state) {
        return state.user;
    }
}

const mutations={
    setUserInfo(state, data){
        for(var k in data) {
            state.user[k] = data[k];
        }
        sessionStorage.user = JSON.stringify(state.user);
    }
}

const actions={
    login({commit},params){
        return new Promise((resolve, reject) => {
            api.login(params).then(res => {
                resolve(res);
            }, res => {
                reject(res);
            });
        })
    },
    //页面刷新时获取用户信息
    setUserInfo({commit}) {
        if(sessionStorage.user){
            commit('setUserInfo',JSON.parse(sessionStorage.user));
        }
    }
}

export default {
    namespaced:true,
    state,
    getters,
    mutations,
    actions
}